package com.zhiyou100.properties;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Properties;

public class Demo1 {

    public static void main(String[] args) throws IOException, InvocationTargetException, IllegalAccessException {
        String path = "./day2_10/src/user.properties";
        //创建一个properties对象
        Properties properties = new Properties();
        //利用流读取文件
        FileInputStream fis = new FileInputStream(path);
        //将输入流加载到properties对象中
        properties.load(fis);
        //获取配置文件的相关信息
        String username = properties.getProperty("user.username");
        System.out.println("username = " + username);
        Object email = properties.get("user.email");
        System.out.println("email = " + email);
        String age = properties.getProperty("user.age", "25");
        System.out.println("age = " + age);
        //设置配置信息
        Object o = properties.setProperty("user.gender", "male");
        System.out.println("o = " + o);
        String gender = properties.getProperty("user.gender");
        System.out.println("gender = " + gender);
        //将自定义的配置信息写到配置文件中（修改）
        FileOutputStream fos = new FileOutputStream(path);
        properties.store(fos,"性别xxxxxx");

        //修改配置信息  根据key修改    注意  输出流需要重新实例化
        properties.setProperty("user.username","薛印");
        properties.store(new FileOutputStream(path),"xxxxxxxxx");
        //获取属性名列表
        Enumeration<?> names = properties.propertyNames();
        User user = new User();
        //遍历属性列表，同时借助反射根据属性key值前缀将数据封装到对应对象中
        while (names.hasMoreElements()) {
            String element = (String) names.nextElement();
            //属性key值前缀
            String prefix = element.substring(0, element.indexOf("."));
            if (prefix.equals("user")) {
                //key值后缀
                String suffix = element.substring(element.indexOf(".") + 1);
                //获取User对象运行时类对象
                Class<? extends User> userClass = user.getClass();
                //获取当前类对象的所有声明的方法
                Method[] methods = userClass.getDeclaredMethods();
                for (Method method : methods) {
                    //获取方法名称
                    String name = method.getName();
                    //判断set方法名称是否和set+key值后缀一致，一致说明可以封装到此对象中
                    if (name.toLowerCase().equals("set"+suffix)) {
                        //获取方法的参数类型，便于后面做数据类型转换
                        Class<?>[] types = method.getParameterTypes();
                        String typeName = null;
                        for (Class<?> type : types) {
                            //获取方法参数类型名称
                            typeName = type.getName();
                        }
                        //获取key值对应的value
                        String s = properties.getProperty(element);
                        //参数类型判断，根据不同类型做不同的转换
                        if (typeName.equals("int")){
                            method.invoke(user,Integer.parseInt(s));
                        }else {
                            method.invoke(user,s);
                        }
                    }
                }
            }
        }
        System.out.println("user = " + user);
    }

}
